/**   
* @Title: UMBinding.js 
* @Package resource/js
* @Description: TODO(用一句话描述该文件做什么) 
* @author xujianhua
* @date 2016年2月24日 下午2:27:38 
* @version V1.0   
*/
var umbinding = {
	/**
	 * 配置
	 */
	settings : {
		model:{}
	},
	
	/**
	 * Model与页面绑定
	 * 约定：页面元素的name为model的key
	 */
	bind : function(data){
		if(data==null){
			return;
		}
		umbinding.settings.model = data;
		//dom赋值
		for(var name in umbinding.settings.model){
			//约定：对象的属性名称定义为dom的name
			var elementName = name;
			var elements = document.getElementsByName(elementName);
			//根据name进行设置
			if(elements!=null&&elements.length>0&&typeof(elements)!="undefined"){
				var length = elements.length;
				//length==1  input-text,textarea,select
				//length>1 input-radio,input-checkbox
				var element = elements[0];
				if(element==null||typeof(element)=="undefined"){
					continue;
				}
				var nodeName = element.nodeName;
				if(nodeName=="INPUT"){//input赋值
					var type = $("input[name='"+elementName+"']").attr("type");
					//不同类型，赋值方式不同
					if(type=="text"){
						umbinding.bindInputText(umbinding.settings.model,elementName);
					}else if(type=="radio"){
						umbinding.bindInputRadio(umbinding.settings.model,name);
					}else if(type=="hidden"){
						umbinding.bindInputHidden(umbinding.settings.model,name);
					}
				}else if(nodeName=="SELECT"){
					umbinding.bindSelect(umbinding.settings.model,name);
				}else if(nodeName=="TEXTAREA"){
					umbinding.bindTextarea(umbinding.settings.model,name);
				}else if(nodeName=="SPAN"){
					umbinding.bindSpan(umbinding.settings.model,name);
				}
			} 
		}
	},
	bindSelect:function(model,name){
		var value = model[name];
		$("select[name='"+name+"']").val(value);
		//修改后同步更新Model
		$("select[name='"+name+"']").on("change",function(){
			var newValue = $("select[name='"+name+"']").val();
			umbinding.settings.model[name] = newValue;
		});
	},

	/**
	 * input text 绑定
	 */
	bindInputText:function(model,name){
		var value = model[name];
		$("input[name='"+name+"']").val(value);
		//修改后同步更新Model
		$("input[name='"+name+"']").on("change",function(){
			var newValue = $("input[name='"+name+"']").val();
			umbinding.settings.model[name] = newValue;
		});
	},

	/**
	 * input radio 绑定
	 */
	 bindInputRadio : function(model,name){
		 var value = model[name];
		$("input[type='radio'][name='"+name+"'][value='"+value+"']").attr("checked",true);
		//修改后同步更新Model
		$("input[type='radio'][name='"+name+"']").on("change",function(){
			var newValue = $("input[type='radio'][name='"+name+"']:checked").val();
			umbinding.settings.model[name] = newValue;
		});
	},
	/**
	 * input radio 绑定
	 */
	 bindInputHidden : function(model,name){
		 var value = model[name];
			$("input[name='"+name+"']").val(value);
			//修改后同步更新Model
			$("input[name='"+name+"']").on("change",function(){
				var newValue = $("input[name='"+name+"']").val();
				umbinding.settings.model[name] = newValue;
			});
	},
	 
	 /**
	  * input radio 绑定
	  */
	bindTextarea : function(model,name){
	 	 var value = model[name];
	 	$("textarea[name='"+name+"']").val(value);
	 	//修改后同步更新Model
	 	$("textarea[name='"+name+"']").on("change",function(){
	 		var newValue =$("textarea[name='"+name+"']").val();
	 		umbinding.settings.model[name] = newValue;
	 	});
	 },
	 bindSpan :function(model,name){
		 var value = model[name];
		 $("span[name='"+name+"']").html(value);
	 },
	 /**
	  * 获取Model，去除值为null内容
	  */
	  getModel: function(){
		  var model = umbinding.settings.model;
		  var newModel = {};
		  for(var key in model){
			  var value =  model[key];
			  if(value!=null&&value!="null"){
				  newModel[key] = value;
			  }
		  }
		  return newModel;
	}
}

